package zcw.com.lib_leet_code;

import java.util.LinkedList;
import java.util.List;

import zcw.com.lib_leet_code.bean.TreeNode;

/**
 * Created by 朱城委 on 2021/9/6.<br><br>
 *
 * 中等：不同的二叉搜索树 II
 */
public class Topic95 {

    public static void main(String[] args) {
        Topic95 instance = new Topic95();

        System.out.println(instance.generateTrees(3));
        System.out.println(instance.generateTrees(1));
    }

    public List<TreeNode> generateTrees(int n) {
        if(n == 0) {
            return new LinkedList<>();
        }

        return helper(1, n);
    }

    private List<TreeNode> helper(int start, int end) {
        List<TreeNode> result = new LinkedList<>();
        if(start > end) {
            result.add(null);
            return result;
        }

        for(int i = start; i <= end; i++) {
            List<TreeNode> leftTree = helper(start, i - 1);
            List<TreeNode> rightTree = helper(i + 1, end);

            for(TreeNode left : leftTree) {
                for(TreeNode right : rightTree) {
                    TreeNode root = new TreeNode(i);
                    root.left = left;
                    root.right = right;
                    result.add(root);
                }
            }
        }

        return result;
    }
}
